home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 August: Tool Chest / Dev.CD Aug 94.toast / Tool Chest / Developer Utilities / Installer 4.0GM SDK / Script Examples / File Search Procedure / FindTargetFile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-10  |  10.4 KB  |  293 lines  |  [TEXT/MPS ]

  1. #ifndef __Files__
  2. #include <Files.h>
  3. #endif
  4.  
  5. #ifndef __GestaltEqu__
  6. #include <GestaltEqu.h>
  7. #endif
  8.  
  9. #ifndef __Memory__
  10. #include <Memory.h>
  11. #endif
  12.  
  13. #ifndef __OSUtils__
  14. #include <OSUtils.h>
  15. #endif
  16.  
  17. #ifndef __Packages__
  18. #include <Packages.h>
  19. #endif
  20.  
  21.  
  22. #ifndef __CallbackDispatcherHeader__
  23. #include "CallbackDispatcherHeader.h"
  24. #endif
  25.  
  26. #ifndef __ActionHandlerHeader__
  27. #include "ActionHandlerHeader.h"
  28. #endif
  29.  
  30. #ifndef __InstallerMemoryFuncsHeader__
  31. #include "InstallerMemoryFuncsHeader.h"
  32. #endif
  33.  
  34. #ifndef __SearchProcedureHeader__
  35. #include "SearchProcedureHeader.h"
  36. #endif
  37.  
  38. void DisplaySearchInfo( SearchProcedurePBPtr pSearchProcedurePBPtr, SearchResult pResultCode );
  39. FoundFileArrayHdl MakeFoundFilesArrayHdl();
  40. OSErr    AddFSSpecToFoundFilesArrayHdl(    FoundFileArrayHdl    pFoundFileArrayHdl,
  41.                                         FSSpec                pFSSpec );
  42.  
  43. SearchResult FindTargetFile( SearchProcedurePBPtr pSearchProcedurePBPtr )
  44. {
  45.     #define            kMaxNumOfMatches    40
  46.     #define            kOptBufferSize        0x100
  47.  
  48.     SearchResult        result;
  49.     FoundFileArrayHdl    myFoundFileArrayHdl;
  50.  
  51.     long                theSysVersionNum;
  52.  
  53.     OSErr                theErr;
  54.     CInfoPBRec            theFirstSearchCriteria;
  55.     CInfoPBRec            theSecondSearchCriteria;
  56.     CSParam                theCatParamBlock;
  57.     FSSpec                theMatchedFSSpecArray[kMaxNumOfMatches];
  58.     char                theOptionalBuffer[kOptBufferSize];
  59.     short                theByteCtr;
  60.     short                theFoundFSSpecCtr;
  61.     FSSpec                theTempFSSpec;
  62.  
  63.     myFoundFileArrayHdl = MakeFoundFilesArrayHdl( pSearchProcedurePBPtr->fCallBackProcPtr );
  64.     
  65.     if( myFoundFileArrayHdl != NULL ) {
  66.     
  67.         Gestalt( 'sysv', &theSysVersionNum );
  68.         if( theSysVersionNum >= 0x00000700 ) {
  69.         
  70.             theCatParamBlock.ioCompletion        = NULL;
  71.             theCatParamBlock.ioNamePtr            = NULL;
  72.             theCatParamBlock.ioVRefNum            = pSearchProcedurePBPtr->fTargetVRefNum;
  73.             
  74.             theCatParamBlock.ioMatchPtr            = (FSSpecArrayPtr)theMatchedFSSpecArray;    /* match array */
  75.             theCatParamBlock.ioReqMatchCount    = kMaxNumOfMatches;                            /* maximum allowable matches */
  76.             theCatParamBlock.ioSearchBits        = fsSBFlFndrInfo + fsSBFlAttrib;            /* search criteria selector */
  77.             theCatParamBlock.ioSearchInfo1        = &theFirstSearchCriteria;                    /* search values and range lower bounds */
  78.             theCatParamBlock.ioSearchInfo2        = &theSecondSearchCriteria;                    /* search values and range upper bounds */
  79.             theCatParamBlock.ioSearchTime        = 0;                                        /* length of time to run search */
  80.             theCatParamBlock.ioCatPosition.initialize    = 0;                                /* current position in the catalog */
  81.             theCatParamBlock.ioOptBuffer        = theOptionalBuffer;                        /* optional performance enhancement buffer */
  82.             theCatParamBlock.ioOptBufSize        = kOptBufferSize;                            /* size of buffer pointed to by ioOptBuffer */
  83.         
  84.             theFirstSearchCriteria.hFileInfo.ioFlFndrInfo.fdType        = pSearchProcedurePBPtr->fFileSpecType;            /* An application */
  85.             theFirstSearchCriteria.hFileInfo.ioFlFndrInfo.fdCreator        = pSearchProcedurePBPtr->fFileSpecCreator;            /* TeachText creator */
  86.             theFirstSearchCriteria.hFileInfo.ioFlAttrib                    = 0x00;                /* A File */
  87.     
  88.             theSecondSearchCriteria.hFileInfo.ioFlFndrInfo.fdType        = 0xFFFFFFFF;        /*the type of the file*/
  89.             theSecondSearchCriteria.hFileInfo.ioFlFndrInfo.fdCreator    = 0xFFFFFFFF;        /*file's creator*/
  90.             theSecondSearchCriteria.hFileInfo.ioFlFndrInfo.fdFlags        = 0x0000;            /*flags ex. hasbundle,invisible,locked, etc.*/
  91.             theSecondSearchCriteria.hFileInfo.ioFlFndrInfo.fdLocation.h    = 0x0000;            /*file's location in folder*/
  92.             theSecondSearchCriteria.hFileInfo.ioFlFndrInfo.fdLocation.v    = 0x0000;            /*file's location in folder*/
  93.             theSecondSearchCriteria.hFileInfo.ioFlFndrInfo.fdFldr        = 0x0000;            /*folder containing file*/
  94.             theSecondSearchCriteria.hFileInfo.ioFlAttrib                = 0x10;                /* Check file/directory bit */
  95.             
  96.             theErr = PBCatSearchSync( &theCatParamBlock );
  97.         
  98.             // Add each found
  99.             for( theFoundFSSpecCtr = 0; theFoundFSSpecCtr < theCatParamBlock.ioActMatchCount; theFoundFSSpecCtr++ ) {
  100.                 theTempFSSpec.vRefNum    = theMatchedFSSpecArray[theFoundFSSpecCtr].vRefNum;
  101.                 theTempFSSpec.parID    = theMatchedFSSpecArray[theFoundFSSpecCtr].parID;
  102.                 
  103.                 for( theByteCtr = 0; theByteCtr <= theMatchedFSSpecArray[theFoundFSSpecCtr].name[0]; theByteCtr++ )
  104.                     theTempFSSpec.name[theByteCtr]    = theMatchedFSSpecArray[theFoundFSSpecCtr].name[theByteCtr];
  105.  
  106.                 AddFSSpecToFoundFilesArrayHdl( myFoundFileArrayHdl,theTempFSSpec );    
  107.             }
  108.         
  109.         }
  110.         else {
  111.         
  112.             // Implement the pre-7.0 search code here
  113.         
  114.         }
  115.             
  116.         
  117.         pSearchProcedurePBPtr->fFoundFilesArray = myFoundFileArrayHdl;
  118.         result = kSearchSuccessful;
  119.  
  120.         DisplaySearchInfo( pSearchProcedurePBPtr, result );
  121.  
  122.     }
  123.     else 
  124.         result = kFatalSearchError;
  125.         
  126.     return result;
  127. }
  128.  
  129.  
  130.  
  131. // ***********************************************************************************************
  132. // ******************************** FoundFileArrayHdl routines ***********************************
  133. // ***********************************************************************************************
  134.  
  135. FoundFileArrayHdl MakeFoundFilesArrayHdl( ProcPtr    pCallBackProcPtr)
  136. {
  137.     return (FoundFileArrayHdl)INewHandle( pCallBackProcPtr, 0 );
  138. }
  139.  
  140. OSErr    AddFSSpecToFoundFilesArrayHdl(    FoundFileArrayHdl    pFoundFileArrayHdl,
  141.                                         FSSpec                pFSSpec )
  142. {
  143.     FSSpecPtr            newFSSpecPtr;
  144.     OSErr                theErr     = noErr;
  145.     Size                orgSize = GetHandleSize( (Handle)pFoundFileArrayHdl );
  146.     
  147.     if( pFoundFileArrayHdl != NULL ) {
  148.         SetHandleSize( (Handle)pFoundFileArrayHdl, orgSize + sizeof( FoundFileRec ) );
  149.         theErr = MemError();
  150.         
  151.         if( theErr == noErr ) {
  152.             newFSSpecPtr = (FSSpecPtr)(StripAddress(*pFoundFileArrayHdl) + orgSize);
  153.             *newFSSpecPtr = pFSSpec;
  154.         }
  155.     }
  156.     
  157.     return theErr;
  158. }
  159.  
  160.  
  161.  
  162. // -------------------------------------------------------------------------------------------------------------------------------------
  163. // The routines below are for displaying the contents of the parameter block passed to the Setup Function.
  164. // Call DisplayPreferenceInfo to write this information to the Installer Debugger.
  165.  
  166. void MakeFilePath( FSSpec* pFileFSSpec, Str255  pFilePath )
  167. {
  168.     
  169.     CInfoPBRec        cPBRec;
  170.     Str255            directoryName = "";
  171.     short            i;
  172.     OSErr            theErr;
  173.  
  174.     // Get the file name
  175.     for( i=0;i<=pFileFSSpec->name[0];i++)
  176.         pFilePath[i] = pFileFSSpec->name[i];
  177.  
  178.     cPBRec.hFileInfo.ioCompletion = NULL;
  179.     cPBRec.hFileInfo.ioNamePtr = directoryName;
  180.     cPBRec.hFileInfo.ioFDirIndex = -1;
  181.     cPBRec.hFileInfo.ioDirID = pFileFSSpec->parID;
  182.     cPBRec.hFileInfo.ioVRefNum = pFileFSSpec->vRefNum;
  183.     theErr = PBGetCatInfo( &cPBRec, 0 );
  184.  
  185.     // Preappend directoryName
  186.     BlockMove( pFilePath + 1, pFilePath + directoryName[0] + 2, pFilePath[0] );
  187.     for( i=1;i<=directoryName[0];i++)
  188.         pFilePath[i] = directoryName[i];
  189.     pFilePath[i] = ':';
  190.     pFilePath[0] += directoryName[0] + 1;
  191.  
  192.     while ( theErr == noErr && cPBRec.dirInfo.ioDrParID != 1 && pFilePath[0] + directoryName[0] + 1 < 255 ) {
  193.  
  194.     // Preappend directoryName
  195.         cPBRec.hFileInfo.ioDirID = cPBRec.dirInfo.ioDrParID;
  196.         cPBRec.hFileInfo.ioFDirIndex = -1;
  197.         theErr = PBGetCatInfo( &cPBRec, 0 );
  198.  
  199.         if( theErr == noErr ) {
  200.             // Preappend directoryName
  201.             BlockMove( pFilePath + 1, pFilePath + directoryName[0] + 2, pFilePath[0] );
  202.             for( i=1;i<=directoryName[0];i++)
  203.                 pFilePath[i] = directoryName[i];
  204.             pFilePath[i] = ':';
  205.             pFilePath[0] += directoryName[0] + 1;
  206.         }
  207.     }
  208.  
  209. }
  210.  
  211. void PrintLine( ProcPtr pCallBackProcPtr, Str255 pParam0, Str255 pParam1, Str255 pParam2, Str255 pParam3 )
  212. {
  213.     long    theResult;
  214.     RegisterScriptAction( pCallBackProcPtr, kDebuggingAction, kGenericDebugActID, pParam0, pParam1, pParam2, pParam3, &theResult );    
  215. }
  216.  
  217. void DisplaySearchInfo( SearchProcedurePBPtr pSearchProcedurePBPtr, SearchResult pResultCode )
  218. {
  219.  
  220. StringPtr                    kBeginCallPart1                    = "\p=>========================== Begin File Search Function Call ====";
  221. StringPtr                    kBeginCallPart3                    = "\p==================================================";
  222.  
  223. StringPtr                    kEndCallPart1                    = "\p-<-------------------------- End File Search Function Call ------ ";
  224. StringPtr                    kEndCallPart3                    = "\p ----------------------------\n";
  225.  
  226. StringPtr                    kResultText                        = "\p       Result Code: ";
  227. StringPtr                    kRefConText                        = "\p            RefCon: ";
  228. StringPtr                    kFileTypeText                    = "\p              Type: ";
  229. StringPtr                    kFileCreatorText                = "\p           Creator: ";
  230. StringPtr                    kFileFileSpecPathText            = "\p    File Spec Path: ";
  231.  
  232. StringPtr                    kFilesFoundTitleText            = "\pFound Files...";
  233. StringPtr                    kNoFilesFoundTitleText            = "\pNo Files Found!";
  234. StringPtr                    kFoundFilePathText                = "\p        Found File:  ";
  235.  
  236. StringPtr                    kTargetFolderPathText            = "\p    Target Folder Path:  ";
  237. StringPtr                    kSystemDiskText                    = "\p      System Disk Name:  ";
  238.  
  239. Str255                        tempNumStr;    
  240. Str255                        tempStr255;    
  241.  
  242. short                        numOfFoundFiles;
  243. short                        foundFileCtr;
  244. FoundFileRec                theFoundFileRec;
  245.  
  246.     // -- Beginning line
  247.     PrintLine( pSearchProcedurePBPtr->fCallBackProcPtr, kBeginCallPart1, kBeginCallPart3, "\p", "\p" );
  248.  
  249.     // —— Print File Path from File Spec
  250.     PrintLine( pSearchProcedurePBPtr->fCallBackProcPtr, kFileFileSpecPathText, pSearchProcedurePBPtr->fFileSpecPath, "\p", "\p" );
  251.     
  252.     // —— Print Type from File Spec
  253.     BlockMove( &(pSearchProcedurePBPtr->fFileSpecType), &(tempNumStr[2]), 4 );
  254.     tempNumStr[1] = '\'';
  255.     tempNumStr[6] = '\'';
  256.     tempNumStr[0] = 6;
  257.     PrintLine( pSearchProcedurePBPtr->fCallBackProcPtr, kFileTypeText, tempNumStr, "\p", "\p" );
  258.     
  259.     // —— Print Creator from File Spec
  260.     BlockMove( &(pSearchProcedurePBPtr->fFileSpecCreator), &(tempNumStr[2]), 4 );
  261.     tempNumStr[1] = '\'';
  262.     tempNumStr[6] = '\'';
  263.     tempNumStr[0] = 6;
  264.     PrintLine( pSearchProcedurePBPtr->fCallBackProcPtr, kFileCreatorText, tempNumStr, "\p", "\p" );
  265.     
  266.     // —— Print RefCon
  267.     NumToString( pSearchProcedurePBPtr->fRefCon, tempNumStr );
  268.     PrintLine( pSearchProcedurePBPtr->fCallBackProcPtr, kRefConText, tempNumStr, "\p", "\p" );
  269.     
  270.     if( pSearchProcedurePBPtr->fFoundFilesArray != NULL ) {
  271.         numOfFoundFiles = GetHandleSize( (Handle)pSearchProcedurePBPtr->fFoundFilesArray ) / sizeof( FoundFileRec);
  272.         
  273.         if( numOfFoundFiles > 0 )
  274.             PrintLine( pSearchProcedurePBPtr->fCallBackProcPtr, kFilesFoundTitleText, "\p", "\p", "\p" );
  275.         else
  276.             PrintLine( pSearchProcedurePBPtr->fCallBackProcPtr, kNoFilesFoundTitleText, "\p", "\p", "\p" );
  277.         
  278.         // Print found file paths
  279.         for( foundFileCtr = 0; foundFileCtr < numOfFoundFiles; foundFileCtr++ ) {
  280.             theFoundFileRec = (*(pSearchProcedurePBPtr->fFoundFilesArray))[foundFileCtr];
  281.             MakeFilePath( (FSSpec*)&theFoundFileRec, tempStr255 );
  282.             PrintLine( pSearchProcedurePBPtr->fCallBackProcPtr, kFoundFilePathText, tempStr255, "\p", "\p" );
  283.         }
  284.     }
  285.  
  286.     // -- Ending line
  287.     NumToString( pResultCode, tempNumStr );
  288.     PrintLine( pSearchProcedurePBPtr->fCallBackProcPtr, kEndCallPart1, kResultText, tempNumStr, kEndCallPart3 );
  289. }
  290.  
  291. #include "ActionHandlerCBGlue.c"
  292. #include "InstallerMemoryCBGlue.c"
  293.